import { defineStore } from 'pinia';

// 定义存储的类型
interface User {
  stuId: string;
  stuClass: string;
  // 可以添加其他用户相关信息
}

// 从 Local Storage 中获取用户数据
function getUserFromLocalStorage(): User | null {
  const userData = localStorage.getItem('user');
  return userData ? JSON.parse(userData) : null;

}

// 创建并导出使用 Pinia 的 store
export const useAuthStore = defineStore('auth', {
  state: () => {
    return {
      pagestate: true,
      pagetitle: "阳哥电源",
      duration: 600,
      up: false,
      topics: sessionStorage.getItem("topics") == null ? [] : JSON.parse(sessionStorage.getItem("topics")),
      nowtopic: sessionStorage.getItem("nowtopic") == null ? 0 : sessionStorage.getItem("nowtopic"),
      correctnum: sessionStorage.getItem("correctnum") == null ? 0 : sessionStorage.getItem("correctnum"),
      user: getUserFromLocalStorage(),
      nowlevel: sessionStorage.getItem("nowlevel") == null ? [] : sessionStorage.getItem("nowlevel"),
      studynum: 0,
      playnum: 0,
      shuaxin: sessionStorage.getItem("nowlevel") == null ? [] : sessionStorage.getItem("nowlevel"),
    };
  },
  actions: {
    setshuaxin(x){
      this.shuaxin = x;
      sessionStorage.setItem("shuaxin", x)
    },
    setplaynum(x) {
      this.playnum = x;
    },
    setstudynum(x) {
      this.studynum = x;
    },
    setnowlevel(x) {
      this.nowlevel = x;
      sessionStorage.setItem("nowlevel", x)
    },
    settopics(topics) {

      sessionStorage.setItem("topics", JSON.stringify(topics))
      this.topics = topics;
    },
    setcorrectnum(x) {
      this.correctnum = x;
      sessionStorage.setItem("correctnum", this.correctnum);
    },
    resetnowtopic() { this.nowtopic = 0; sessionStorage.setItem("nowtopic", this.nowtopic); },
    addnowtopic() {
      this.nowtopic++;
      sessionStorage.setItem("nowtopic", this.nowtopic);
    },
    setpagetitle(s) {
      this.pagetitle = s;
    },
    setpagetrue() {
      this.pagestate = true;
    },
    setpagefalse() {
      this.pagestate = false;
    },
    setbackground(x: any) {
      this.background = x;
    },
    setduration(x: number) {
      this.duration = x;
    },
    setuptrue() {
      this.up = true;
    },
    setupfalse() {
      this.up = false;
    },
    setuser(value: User) {
      this.user = value;
      localStorage.setItem('user', JSON.stringify(value));
    },
    logout() {
      this.user = null;
      localStorage.removeItem('user');
    },
  },
});
